package com.hd.trans.utils;


import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.hd.trans.ui.base.AppContextProvider;

public class ScreenUtil {


    private static int screenWidth;
    private static int screenHeight;
    private static int navigationHeight = 0;
    private static int statusBarHeight = 0;
    public static void init(Context context){
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    /**
     * 获取屏幕宽度（不包含底部导航栏）
     */
    public static int getScreenWidth(Context context) {
        if(screenWidth <= 0){
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
        }
        return screenWidth;
    }

    public static int getScreenWidth() {
        return getScreenWidth(AppContextProvider.getContext());
    }

    /**
     * 获得屏幕高度（不包含底部导航栏）
     */
    public static int getScreenHeight(Context context) {
        if(screenHeight <= 0){
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenHeight = dm.heightPixels;
        }
        return screenHeight;
    }
    public static int getScreenHeight() {
        return getScreenHeight(AppContextProvider.getContext());
    }

    /**
     * 获取屏幕真实宽度（包含底部导航栏）
     */
    public static int getScreenRealWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final Display display = windowManager.getDefaultDisplay();
        Point outPoint = new Point();
        if (Build.VERSION.SDK_INT >= 19) {
            // 可能有虚拟按键的情况
            display.getRealSize(outPoint);
        } else {
            // 不可能有虚拟按键
            display.getSize(outPoint);
        }
        int mRealSizeWidth;//手机屏幕真实宽度
        mRealSizeWidth = outPoint.x;
        return mRealSizeWidth;
    }

    /**
     * 获得屏幕真实高度（包含底部导航栏）
     */
    public static int getScreenRealHeight(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final Display display = windowManager.getDefaultDisplay();
        Point outPoint = new Point();
        if (Build.VERSION.SDK_INT >= 19) {
            // 可能有虚拟按键的情况
            display.getRealSize(outPoint);
        } else {
            // 不可能有虚拟按键
            display.getSize(outPoint);
        }
        int mRealSizeHeight;//手机屏幕真实高度
        mRealSizeHeight = outPoint.y;
        return mRealSizeHeight;
    }


    public static int getDimensionOfDp(int dp) {
        return getDimensionOfDp(AppContextProvider.getContext(),dp);
    }

    public static int getDimensionOfDp(Context context, int dp) {
        int xxh = dp * 3;
        String idName = "q"+xxh;
        int id = context.getResources().getIdentifier(idName, "dimen", context.getPackageName());
        if (id > 0) {
            return context.getResources().getDimensionPixelSize(id);
        } else {
            return dp2px(context, dp);
        }
    }

    public static int dp2px(float dpValue) {
        return dp2px(AppContextProvider.getContext(), dpValue);
    }
    public static int dp2px(Context context,float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}